{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "pro = ts.pro_api()\n",
    "data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>000001</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>深圳</td>\n",
       "      <td>银行</td>\n",
       "      <td>19910403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>000002</td>\n",
       "      <td>万科A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>全国地产</td>\n",
       "      <td>19910129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>000004</td>\n",
       "      <td>国华网安</td>\n",
       "      <td>深圳</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>19910114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>000006</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>区域地产</td>\n",
       "      <td>19920427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000007.SZ</td>\n",
       "      <td>000007</td>\n",
       "      <td>全新好</td>\n",
       "      <td>深圳</td>\n",
       "      <td>其他商业</td>\n",
       "      <td>19920413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5404</th>\n",
       "      <td>920111.BJ</td>\n",
       "      <td>920111</td>\n",
       "      <td>聚星科技</td>\n",
       "      <td>浙江</td>\n",
       "      <td>电气设备</td>\n",
       "      <td>20241111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5405</th>\n",
       "      <td>920116.BJ</td>\n",
       "      <td>920116</td>\n",
       "      <td>星图测控</td>\n",
       "      <td>安徽</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20250102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5406</th>\n",
       "      <td>920118.BJ</td>\n",
       "      <td>920118</td>\n",
       "      <td>太湖远大</td>\n",
       "      <td>浙江</td>\n",
       "      <td>塑料</td>\n",
       "      <td>20240822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5407</th>\n",
       "      <td>920128.BJ</td>\n",
       "      <td>920128</td>\n",
       "      <td>胜业电气</td>\n",
       "      <td>广东</td>\n",
       "      <td>元器件</td>\n",
       "      <td>20241129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5408</th>\n",
       "      <td>689009.SH</td>\n",
       "      <td>689009</td>\n",
       "      <td>九号公司-WD</td>\n",
       "      <td>北京</td>\n",
       "      <td>摩托车</td>\n",
       "      <td>20201029</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5409 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code  symbol     name area industry list_date\n",
       "0     000001.SZ  000001     平安银行   深圳       银行  19910403\n",
       "1     000002.SZ  000002      万科A   深圳     全国地产  19910129\n",
       "2     000004.SZ  000004     国华网安   深圳     软件服务  19910114\n",
       "3     000006.SZ  000006     深振业A   深圳     区域地产  19920427\n",
       "4     000007.SZ  000007      全新好   深圳     其他商业  19920413\n",
       "...         ...     ...      ...  ...      ...       ...\n",
       "5404  920111.BJ  920111     聚星科技   浙江     电气设备  20241111\n",
       "5405  920116.BJ  920116     星图测控   安徽     软件服务  20250102\n",
       "5406  920118.BJ  920118     太湖远大   浙江       塑料  20240822\n",
       "5407  920128.BJ  920128     胜业电气   广东      元器件  20241129\n",
       "5408  689009.SH  689009  九号公司-WD   北京      摩托车  20201029\n",
       "\n",
       "[5409 rows x 6 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pro.namechange(ts_code='600848.SSE', fields='ts_code,name,start_date,end_date,change_reason')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>name</th>\n",
       "      <th>start_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>change_reason</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [ts_code, name, start_date, end_date, change_reason]\n",
       "Index: []"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# 读取CSV文件\n",
    "df = pd.read_csv('../data/600000_daily.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>exchange</th>\n",
       "      <th>datetime</th>\n",
       "      <th>interval</th>\n",
       "      <th>volume</th>\n",
       "      <th>turnover</th>\n",
       "      <th>open_interest</th>\n",
       "      <th>open_price</th>\n",
       "      <th>high_price</th>\n",
       "      <th>low_price</th>\n",
       "      <th>close_price</th>\n",
       "      <th>vt_symbol</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-14 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>621541.63</td>\n",
       "      <td>629562.548</td>\n",
       "      <td>0</td>\n",
       "      <td>10.04</td>\n",
       "      <td>10.22</td>\n",
       "      <td>10.00</td>\n",
       "      <td>10.18</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-11 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>562461.96</td>\n",
       "      <td>564930.748</td>\n",
       "      <td>0</td>\n",
       "      <td>10.06</td>\n",
       "      <td>10.12</td>\n",
       "      <td>9.92</td>\n",
       "      <td>10.07</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-10 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>781080.87</td>\n",
       "      <td>781663.686</td>\n",
       "      <td>0</td>\n",
       "      <td>10.18</td>\n",
       "      <td>10.18</td>\n",
       "      <td>9.91</td>\n",
       "      <td>10.00</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-09 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>997145.63</td>\n",
       "      <td>997750.443</td>\n",
       "      <td>0</td>\n",
       "      <td>10.19</td>\n",
       "      <td>10.19</td>\n",
       "      <td>9.86</td>\n",
       "      <td>10.01</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-08 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>1494229.15</td>\n",
       "      <td>1501459.278</td>\n",
       "      <td>0</td>\n",
       "      <td>9.77</td>\n",
       "      <td>10.29</td>\n",
       "      <td>9.75</td>\n",
       "      <td>10.20</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>273</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2024-02-27 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>248622.46</td>\n",
       "      <td>176742.732</td>\n",
       "      <td>0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>7.15</td>\n",
       "      <td>7.06</td>\n",
       "      <td>7.14</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>274</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2024-02-26 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>424830.70</td>\n",
       "      <td>304097.864</td>\n",
       "      <td>0</td>\n",
       "      <td>7.26</td>\n",
       "      <td>7.28</td>\n",
       "      <td>7.09</td>\n",
       "      <td>7.10</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>275</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2024-02-23 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>379396.00</td>\n",
       "      <td>276331.224</td>\n",
       "      <td>0</td>\n",
       "      <td>7.21</td>\n",
       "      <td>7.33</td>\n",
       "      <td>7.20</td>\n",
       "      <td>7.28</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2024-02-22 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>336652.09</td>\n",
       "      <td>242967.428</td>\n",
       "      <td>0</td>\n",
       "      <td>7.24</td>\n",
       "      <td>7.27</td>\n",
       "      <td>7.18</td>\n",
       "      <td>7.21</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>277</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2024-02-21 15:00:00+08:00</td>\n",
       "      <td>d</td>\n",
       "      <td>588095.54</td>\n",
       "      <td>426038.720</td>\n",
       "      <td>0</td>\n",
       "      <td>7.11</td>\n",
       "      <td>7.32</td>\n",
       "      <td>7.09</td>\n",
       "      <td>7.24</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>278 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     symbol exchange                   datetime interval      volume  \\\n",
       "0    600000      SSE  2025-04-14 15:00:00+08:00        d   621541.63   \n",
       "1    600000      SSE  2025-04-11 15:00:00+08:00        d   562461.96   \n",
       "2    600000      SSE  2025-04-10 15:00:00+08:00        d   781080.87   \n",
       "3    600000      SSE  2025-04-09 15:00:00+08:00        d   997145.63   \n",
       "4    600000      SSE  2025-04-08 15:00:00+08:00        d  1494229.15   \n",
       "..      ...      ...                        ...      ...         ...   \n",
       "273  600000      SSE  2024-02-27 15:00:00+08:00        d   248622.46   \n",
       "274  600000      SSE  2024-02-26 15:00:00+08:00        d   424830.70   \n",
       "275  600000      SSE  2024-02-23 15:00:00+08:00        d   379396.00   \n",
       "276  600000      SSE  2024-02-22 15:00:00+08:00        d   336652.09   \n",
       "277  600000      SSE  2024-02-21 15:00:00+08:00        d   588095.54   \n",
       "\n",
       "        turnover  open_interest  open_price  high_price  low_price  \\\n",
       "0     629562.548              0       10.04       10.22      10.00   \n",
       "1     564930.748              0       10.06       10.12       9.92   \n",
       "2     781663.686              0       10.18       10.18       9.91   \n",
       "3     997750.443              0       10.19       10.19       9.86   \n",
       "4    1501459.278              0        9.77       10.29       9.75   \n",
       "..           ...            ...         ...         ...        ...   \n",
       "273   176742.732              0        7.07        7.15       7.06   \n",
       "274   304097.864              0        7.26        7.28       7.09   \n",
       "275   276331.224              0        7.21        7.33       7.20   \n",
       "276   242967.428              0        7.24        7.27       7.18   \n",
       "277   426038.720              0        7.11        7.32       7.09   \n",
       "\n",
       "     close_price   vt_symbol  \n",
       "0          10.18  600000.SSE  \n",
       "1          10.07  600000.SSE  \n",
       "2          10.00  600000.SSE  \n",
       "3          10.01  600000.SSE  \n",
       "4          10.20  600000.SSE  \n",
       "..           ...         ...  \n",
       "273         7.14  600000.SSE  \n",
       "274         7.10  600000.SSE  \n",
       "275         7.28  600000.SSE  \n",
       "276         7.21  600000.SSE  \n",
       "277         7.24  600000.SSE  \n",
       "\n",
       "[278 rows x 12 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(datetime.datetime(2025, 4, 14, 12, 2, 59, 804040),\n",
       " datetime.datetime(2025, 4, 15, 12, 2, 59, 804040))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime, timedelta\n",
    "\n",
    "start_time = datetime.now()-timedelta(days=1)\n",
    "end_time = datetime.now()\n",
    "start_time,end_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>exchange</th>\n",
       "      <th>datetime</th>\n",
       "      <th>interval</th>\n",
       "      <th>volume</th>\n",
       "      <th>turnover</th>\n",
       "      <th>open_interest</th>\n",
       "      <th>open_price</th>\n",
       "      <th>high_price</th>\n",
       "      <th>low_price</th>\n",
       "      <th>close_price</th>\n",
       "      <th>vt_symbol</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>600000</td>\n",
       "      <td>SSE</td>\n",
       "      <td>2025-04-14 15:00:00</td>\n",
       "      <td>d</td>\n",
       "      <td>621541.63</td>\n",
       "      <td>629562.548</td>\n",
       "      <td>0</td>\n",
       "      <td>10.04</td>\n",
       "      <td>10.22</td>\n",
       "      <td>10.0</td>\n",
       "      <td>10.18</td>\n",
       "      <td>600000.SSE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   symbol exchange            datetime interval     volume    turnover  \\\n",
       "0  600000      SSE 2025-04-14 15:00:00        d  621541.63  629562.548   \n",
       "\n",
       "   open_interest  open_price  high_price  low_price  close_price   vt_symbol  \n",
       "0              0       10.04       10.22       10.0        10.18  600000.SSE  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df中时间范围在start_time和end_time之间的数据\n",
    "df[\"datetime\"] = pd.to_datetime(df[\"datetime\"]).dt.tz_localize(None)\n",
    "df_btw = df[(df[\"datetime\"] >= start_time) & (df[\"datetime\"] <= end_time)]\n",
    "df_btw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cal_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20250421</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   cal_date\n",
       "0  20250421"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from vnpy.trader.constant import Exchange\n",
    "import tushare as ts\n",
    "\n",
    "pro = ts.pro_api()\n",
    "\n",
    "calendar = pro.trade_cal(\n",
    "                    exchange=Exchange.SSE.value,\n",
    "                    start_date='20250421',\n",
    "                    end_date='20250421',\n",
    "                    is_open=\"1\",\n",
    "                    fields='cal_date'\n",
    "                )\n",
    "\n",
    "calendar"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vnpy",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
